package com.demo.config;


import com.demo.Application;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import org.jboss.vfs.VFS;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.VfsResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;

//@ImportResource(locations = "classpath:applicationContext-*.xml")
@Controller
@SpringBootApplication(scanBasePackageClasses = Application.class)
public class ApplicationContextConfig {

    @RequestMapping("/")
    public String home() {
        return "redirect:/hello/index";
    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {

        return (container -> {
            ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/webapp/static/pages/error/401.html");
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/webapp/static/pages/error/404.html");
            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/webapp/static/pages/error/500.html");

            container.addErrorPages(error401Page, error404Page, error500Page);
        });
    }

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(ApplicationContextConfig.class, args);
        String location = "classpath:META-INF/mp/paycli/pack/*";
        Resource[] resources = context.getResources(location);
        Arrays.stream(resources).forEach(resource -> System.err.println(resource));
        ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
        Resource[] source = resourceLoader.getResources(location);
        //创建一个合适的Configration对象
        Configuration configuration = new Configuration();
        configuration.setObjectWrapper(new DefaultObjectWrapper());
        configuration.setDefaultEncoding("UTF-8");
        for (int i = 0; i < source.length; i++) {
            Resource resource = source[i];
            System.out.println(resource.getFilename() + "--" + resource.getURL());
            VfsResource vfs = new VfsResource(resource);
            System.err.println(vfs.getFile());
            Template template = configuration.getTemplate(resource.getURL().getPath());
            System.err.println(template);
            VFS.mountZip()
        }

    }
}
